/*
 * This file is part of the Refran Library
 * Copyright (C) 2009 César Enrique García
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

/** @file
 * $Author: cquike $
 * $Date: 2010-09-29 10:20:22 +0000 (Wed, 29 Sep 2010) $
 * $Revision: 214 $
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <utility>

#include "Error.h"
#include "Logger.h"


Refran::Logger::Logger(const std::string& name) :
  m_logger(&log4cpp::Category::getInstance(name))
{
    initDefaultLogger_();
}

Refran::Logger::~Logger()
{
    m_logger->shutdown();
}

void Refran::Logger::initDefaultLogger_()
{
    m_logger->addAppender(*Refran::Logger::defaultStdoutAppender);
}

void Refran::Logger::setAppender(log4cpp::Appender& appender)
{
    log4cpp::Appender* currentAppender =  m_logger->getAppender();
    m_logger->addAppender(appender);
    m_logger->removeAppender(currentAppender);
}


void Refran::Logger::setLogger(const std::string& newLoggerName)
{
    log4cpp::Category* newLogger;
    newLogger = log4cpp::Category::exists(newLoggerName);
    if(newLogger == NULL)
        throw Error(__func__,"New logger does not exist yet");
    m_logger = newLogger;
}

//Maybe one can use a iostream which is not std::cout, which can cause problems with valgrind/drd, since it is kind of not thread-safe 
//http://valgrind.org/docs/manual/drd-manual.html#drd-manual.effective-use
//Maybe to include <iosfwd> is enough:
//http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt11ch24.html
std::unique_ptr<log4cpp::OstreamAppender> Refran::Logger::defaultStdoutAppender(
    new log4cpp::OstreamAppender("StdoutAppender", &std::cout));
